package com.example.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.travel.common.AjaxResult;
import com.example.travel.common.Constants;
import com.example.travel.common.TableDataInfo;
import com.example.travel.domain.entity.Dict;
import com.example.travel.domain.entity.Menu;
import com.example.travel.domain.request.PageQuery;
import com.example.travel.mapper.DictMapper;
import com.example.travel.service.MenuService;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author : LinQingLiang
 * @file : MenuController.java
 * @Since : 2023/1/30 21:23
 */

@RestController
@RequestMapping("/menu")
@Api(tags = "菜单权限")
public class MenuController extends BaseController{

    @Autowired
    private MenuService menuService;

    @Autowired
    private DictMapper dictMapper;

    //分页查询
    @GetMapping("list")
    public TableDataInfo list(@Validated PageQuery pageQuery, Menu menu){
//        User currentUser = TokenUtils.getUser();
        Page page1 = menuService.page(menu, pageQuery.getPageNum(),pageQuery.getPageSize());
        return getDataTable(page1.getRecords(),page1.getTotal());
    }

    @GetMapping
    public AjaxResult findAll(@Param("name") String name){
        return AjaxResult.success(menuService.findMenus(name));
    }

    //添加
    @PostMapping("add")
    public AjaxResult create(@RequestBody Menu menu){
        return AjaxResult.success(menuService.save(menu));
    }

    //修改前查询
    @GetMapping("{menuId}")
    public AjaxResult selectById(@PathVariable Integer menuId){
        return AjaxResult.success(menuService.getById(menuId));
    }

    @PutMapping("updateMenu")
    public AjaxResult updateMenu(@RequestBody Menu menu){
        QueryWrapper<Menu> wrapper = new QueryWrapper();
        wrapper.eq("name",menu.getName());
        Menu menu1 = menuService.getOne(wrapper);
        if (menu1 != null && !(menu1.getName()).equals(menu.getName())){
            return AjaxResult.error("菜单名已经存在不能进行修改");
        }
        return toAjax(menuService.updateById(menu));
    }

    //删除个人
    @DeleteMapping("deleteMenu/{menuId}")
    public AjaxResult deleteMenu(@PathVariable Integer menuId){
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Menu::getId,menuId);
        return AjaxResult.success(menuService.remove(lambdaQueryWrapper));
    }

    //批量删除
    @DeleteMapping("deleteMenus/{menuIds}")
    public AjaxResult deleteMenus(@PathVariable List<Integer> menuIds){
        return AjaxResult.success(menuService.removeBatchByIds(menuIds));
    }


    @GetMapping("/icons")
    public AjaxResult getIcons(){
        QueryWrapper<Dict> queryWrapper = new QueryWrapper();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return AjaxResult.success(dictMapper.selectList(queryWrapper));
    }

    @GetMapping("/ids")
    public AjaxResult findAllMenuIds(){
        return AjaxResult.success(menuService.list().stream().map(Menu::getId));
    }
}
